//
//  DYBaseRequest.m
//  DeyiLife
//
//  Created by dy on 15/5/19.
//  Copyright (c) 2015年 Deyi. All rights reserved.
//

#import "DYBaseRequest.h"

@implementation DYBaseRequest

- (YTKRequestMethod )requestMethod
{
    return YTKRequestMethodPost;
}

- (NSDictionary *)requestHeaderFieldValueDictionary {
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    [dict setValue:[UserData usertoken] forKey:@"Auth"];
    DYLog(@"Request Token:%@", [UserData usertoken]);
    return dict;
}

//TODO:超时设置
- (NSTimeInterval)requestTimeoutInterval
{
    return 15;
}

- (id)requestArgument
{
    return self.requestPostDic;
}

- (YTKRequestSerializerType)requestSerializerType
{
    return YTKRequestSerializerTypeJSON;
}


- (NSInteger)cacheTimeInSeconds
{
    if(self.cacheTimeInt>0)
    {
        return self.cacheTimeInt;
    }
    
    return -1;
}



- (AFConstructingBlock)constructingBodyBlock
{
    if(self.postFileKeyName)
    {
        
        return ^(id<AFMultipartFormData> formData)
        {
            if(self.postFileType == nil)
            {
                
                if([self.postFileData.accessibilityValue isEqualToString:@"gif"])
                {
                    self.postFileType = @"image/gif";
                }
                else
                {
                    self.postFileType = @"image/jpeg";
                }
                
            }
            
            if (self.postImages.count<=0&&self.postFileData)
            {
                [formData appendPartWithFileData:self.postFileData name:self.postFileKeyName fileName:@"file" mimeType:self.postFileType];
            }
            
            else
            {
                for (int i=0;i<self.postImages.count; i++) {
                    UIImage *image=self.postImages[i];
                    if(image.size.width/image.size.height > 16/9.00f)
                    {
                        image = [DYImageHelper image:image rotation:UIImageOrientationLeft];
                        
                    }
                    if(image.size.width > 600)
                    {
                        image = [DYImageHelper resizeImage:image targetSize:CGSizeMake(600, image.size.height*600.00f/image.size.width)];
                        
                    }
                    NSData *data = UIImageJPEGRepresentation(image, 0.5f);
                    [formData appendPartWithFileData:data name:self.postFileKeyName fileName:[NSString stringWithFormat:@"%d.jpg",i] mimeType:self.postFileType];
                }
                

            }
           
        };
        
    }
    else
    {
        return nil;
    }
    
}

- (void)dyStartWithCompletionBlockWithSuccess:(void (^)(YTKBaseRequest *))success error:(void (^)(YTKBaseRequest *))error failure:(void (^)(YTKBaseRequest *))failure
{
//    self.successBlock=success;
//    self.errorBlock=error;
//    self.failBlock=failure;
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    [self startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        //NSString *testString = request.responseString;
        NSDictionary *testDic = request.responseJSONObject;
        DYLog(@"request result:%@", request.responseString);
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
        if(self.cacheJson&&!self.ignoreCache)
        {
           if([[self.cacheJson objectForKey:@"code"] intValue]== 200)
           {
               success(request);
           }
            
           else
           {
               if([[request.responseJSONObject objectForKey:@"code"] isEqualToString:@"901"]) {
               } else {
                   [ViewFactory showInfoMsg:[request.responseJSONObject objectForKey:@"msg"]];
               }
               error(request);
           }
        }
        else
        {
            NSDictionary *dicResponse = request.responseJSONObject;
           if([[request.responseJSONObject objectForKey:@"code"] isEqualToString:@"200"])
           {
               success(request);
               
           }
           else
           {
                NSString *requestString = request.responseString;
                DYLog(@"error request:%@", requestString);
                [ViewFactory showInfoMsg:[request.responseJSONObject objectForKey:@"msg"]];
                error(request);
           }
        }
      
   } failure:^(YTKBaseRequest *request) {
       [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
       failure(request);
   }];
}

- (void)dyBgStartWithCompletionBlockWithSuccess:(void (^)(YTKBaseRequest *))success error:(void (^)(YTKBaseRequest *))error failure:(void (^)(YTKBaseRequest *))failure
{
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    [self startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSDictionary *getDic = request.responseJSONObject;
        int error_code = [[getDic valueForKey:@"code"] intValue];
        if(getDic && error_code == 100) {
            //DYLog(@"request result:%@", request.responseString);
            [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
            success(request);
        } else {
            DYLog(@"request result:%@", request.responseString);
            //[ViewFactory showErrorStrMsg:@"操作失败！"];
            [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
            error(request);
        }
    } failure:^(YTKBaseRequest *request) {
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
        failure(request);
    }];
}

- (void)dyOtherStartWithCompletionBlockWithSuccess:(void (^)(YTKBaseRequest *request))success
                                             error:(void (^)(YTKBaseRequest *request))error
                                           failure:(void (^)(YTKBaseRequest *request))failure {
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    [self startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
        success(request);
    } failure:^(YTKBaseRequest *request) {
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
        failure(request);
    }];
}

- (void)dyBgTimeUploadStartWithCompletionBlockWithSuccess:(void (^)(YTKBaseRequest *request))success
                                                    error:(void (^)(YTKBaseRequest *request))error
                                                  failure:(void (^)(YTKBaseRequest *request))failure {
    self.ignoreCache = YES;
    [self startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        if(self.cacheJson) {
            if([[self.cacheJson objectForKey:@"code"] intValue]== 200) {
                DYLog(@"request result:%@", request.responseString);
                success(request);
            } else {
                DYLog(@"request result:%@", request.responseString);
                error(request);
            }
        } else {
            if([[request.responseJSONObject objectForKey:@"code"] isEqualToString:@"200"]) {
                DYLog(@"request result:%@", request.responseString);
                success(request);
            } else {
                DYLog(@"request result:%@", request.responseString);
                error(request);
            }
        }
    } failure:^(YTKBaseRequest *request) {
        DYLog(@"request result:%@", request.responseString);
        failure(request);
    }];
}


@end
